﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeoVision.Addin.Core.Spatial
{
    public class GeoShapeList: IEnumerable<GeoShape>
    {
        public double XMin { get; private set; }
        public double XMax { get; private set; }
        public double YMin { get; private set; }
        public double YMax { get; private set; }

        public bool DisableMinMaxCalculation { get; set; }

        public GeoShapeList()
        {
            XMax = double.MinValue;
            YMax = double.MinValue;
            XMin = double.MaxValue;
            YMin = double.MaxValue;
        }

        private List<GeoShape> internalList = new List<GeoShape>(100);

        public IEnumerator<GeoShape> GetEnumerator()
        {
            return internalList.GetEnumerator();
        }

        public void Add(GeoShape shape)
        {
            if (!DisableMinMaxCalculation)
            {
                foreach (var segment in shape.Segments)
                {
                    double _xMax = segment.Points.Max(u => u.Long);
                    double _xMin = segment.Points.Min(u => u.Long);
                    double _yMax = segment.Points.Max(u => u.Lat);
                    double _yMin = segment.Points.Min(u => u.Lat);
                    if (XMax < _xMax)
                        XMax = _xMax;
                    if (XMin > _xMin)
                        XMin = _xMin;
                    if (YMax < _yMax)
                        YMax = _yMax;
                    if (YMin > _yMin)
                        YMin = _yMin;
                }
            }
            internalList.Add(shape);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return internalList.GetEnumerator();
        }
    }
}
